<?php

namespace App\Repositories\Album;

use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;
use Prettus\Repository\Eloquent\BaseRepository;
use Prettus\Repository\Criteria\RequestCriteria;
use App\Repositories\Album\AlbumToAlbumRepository;
use App\Models\Album\AlbumToAlbum;
use App\Validators\Album\AlbumToAlbumValidator;
use Prettus\Repository\Traits\CacheableRepository;

/**
 * Class AlbumToAlbumRepositoryEloquent.
 *
 * @package namespace App\Repositories\Album;
 */
class AlbumToAlbumRepositoryEloquent extends BaseRepository implements AlbumToAlbumRepository
{

    use CacheableRepository;

    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return AlbumToAlbum::class;
    }

    

    /**
     * Boot up the repository, pushing criteria
     */
    public function boot()
    {
        $this->pushCriteria(app(RequestCriteria::class));
    }

    /**
     * @author Jerry
     * @param int $templateId
     * @return mixed
     */
    public function findAlbumsByTemplateId(int $templateId)
    {
        $where = [
            'ta.template_id'   =>  $templateId,
        ];

        $field = [
            'a.id',
            'a.name',
            'a.small',
            'a.description'
        ];

        $expiresAt = Carbon::now()->addHour(1);
        return Cache::remember("album:template{$templateId}", $expiresAt, function () use ($field, $where) {
           return \DB::table('album_template_albums as ta')
                ->leftJoin('album as a', 'ta.value', '=', 'a.id')
                ->where($where)
                ->get($field)
                ->toArray();
        });
    }

    /**
     * @author Jerry
     * @param int $id
     * @param int $type
     * @return int
     */
    public function isTemplateByAlbumId(int $id, int $type = 2)
    {
        $where = [
            'a.id'      =>  $id,
            'a_t.type'  =>  $type,
        ];

        $count = \DB::table('album as a')
            ->leftJoin('album_template as a_t', 'a.id', '=', 'a_t.album_id')
            ->where($where)
            ->select()
            ->count();

        return $count;
    }

    /**
     * 获取该专题下商品图片
     * @author Jerry
     * @param int $albumId
     * @param int $type
     * @return array
     */
    public function findAlbumGoodsImageByAlbumId(int $albumId, int $type = 2)
    {
        $where = [
            'a.id'      =>  $albumId,
            'a_t.type'  =>  $type,
        ];

        $field = [
            'g.original_img'
        ];

        $expiresAt = Carbon::now()->addHour(1);
        return Cache::remember("album:albumImage{$albumId}", $expiresAt, function () use ($field, $where) {
            return \DB::table('album as a')
                ->leftJoin('album_template as a_t', 'a.id', '=', 'a_t.album_id')
                ->leftJoin('album_template_goods as ag', 'a_t.id', '=', 'ag.template_id')
                ->leftJoin('goods as g', 'ag.goods_id', '=', 'g.goods_id')
                ->where($where)
                ->limit(4)
                ->get($field)
                ->toArray();
        });
    }
    
}
